package cn.zirao

import android.content.Context
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

import java.util.LinkedList

import zirao.base.activity.BaseActivity
import cn.zirao.entity.ItemEntity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity(R.layout.activity_main, 0) {

    private var mAdapter: MyAdapter? = null

    override fun initView() {
        mAdapter = MyAdapter(this)
    }

    override fun initListener() {

    }

    override fun initData() {
        val list = LinkedList<ItemEntity>()
        var itemEntity = ItemEntity()
        itemEntity.name = "Base测试"
        itemEntity.clz = BaseTestActivity::class.java
        list.add(itemEntity)

        itemEntity = ItemEntity()
        itemEntity.name = "Http测试"
        itemEntity.clz = HttpTestActivity::class.java
        list.add(itemEntity)

        itemEntity = ItemEntity()
        itemEntity.name = "Recycler测试"
        itemEntity.clz = RecyclerActivity::class.java
        list.add(itemEntity)

        mAdapter!!.setListData(list)
        main_list_rv!!.layoutManager = LinearLayoutManager(this)
        main_list_rv!!.adapter = mAdapter

        main_list_rv!!.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
    }

    override fun widgetClick(view: View) {

    }

    override fun onBackPressed() {

    }

    internal inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var name: TextView? = null
    }

    internal inner class MyAdapter(private val context: Context) : RecyclerView.Adapter<MyViewHolder>() {
        private var list: LinkedList<ItemEntity>? = null

        fun setListData(list: LinkedList<ItemEntity>) {
            this.list = list
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view = LayoutInflater.from(context).inflate(R.layout.main_rv_item, parent, false)
            val holder = MyViewHolder(view)
            holder.name = view.findViewById(R.id.main_rv_item_name)
            return holder
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            val local = list!![position]

            holder.name!!.text = local.name
            holder.itemView.setOnClickListener { openActivity(local.clz) }
        }

        override fun getItemCount(): Int {
            return list!!.size
        }
    }
}
